package edu.biwu.cookie02;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

/**
 * 设置cookie的path
 * public void setPath(String uri):手动设置cookie的路径
 *
 * 设置一个Cookie: gender=male, 设置路径: /day07-cookie-session
 *   访问/day07-cookie-session/path01/getCookie01:  携带
 *   访问/day07-cookie-session/path01/path02/getCookie02: 携带
 *   访问/day07-cookie-session/getCookie03:  携带
 *
 *    如果请求的路径是cookie的路径 或者是 Cookie路径的子级路径才会携带这个cookie
 */
@WebServlet("/path01/setCookie02")
public class SetCookie02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = new Cookie("gender", "male");
        //cookie.setPath("/day07-cookie-session");
        cookie.setPath(request.getContextPath());// getContextPath 获取的就是: /day07-cookie-session
        response.addCookie(cookie);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
